For packages that will be used here you may have to go to the terminal or R GUI you use and set these appropriately:
In [26]:
# add a local lib to the R library path (for permission purposes)
local_lib = '/Users/micheleenharris/lib/R' # this must exist!
.libPaths(c(.libPaths(), local_lib))
# package to grab url data
install.packages('RCurl', repos = "http://cloud.r-project.org", lib = local_lib)
# some interactive plotting packages
install.packages(c("DT", "htmlwidgets", "leaflet"),
repos = "http://cloud.r-project.org",
lib = local_lib)
Let's load a list of airport data from https://github.com/jpatokal/openflights
In [9]:
# TIP: often you'll want some data externally, but can not upload a file to jupyter system
# In this case I pull directly from a raw file online (like a repo file here)
library(RCurl)
airports <- getURL("https://raw.githubusercontent.com/jflam/VSDemos/master/RTVSDemos/airports.dat")
airports <- read.csv(text = airports,
header = FALSE)
colnames(airports) <- c("ID", "name", "city", "country", "IATA_FAA", "ICAO", "lat", "lon",
"altitude", "timezone", "DST", "Region")
This data set contains airports from all over the world. Let's get only the airports from the United States.
In [12]:
usa_airports <- subset(airports, country == "United States")
# DT is a really nifty interactive table library with pagination
library(DT)
table <- datatable(usa_airports[,c("name", "city", "country", "IATA_FAA", "lat", "lon", "altitude")])
# Here is the trick to get an interactive graphic to work in notebooks
# 1. load htmlwidgets
# 2. save interactive object to an html file with saveWidget
# 3. load the html back into the notebook and into an iframe element for display with IRdisplay
library(htmlwidgets) # lots of cool widgets associated w/ this (http://www.htmlwidgets.org/)
fname <- "something.html"
# selfcontained = FASLE means: save a file with external resources placed in an adjacent directory
# do this just in case there are external resources
saveWidget(table, file = fname, selfcontained = F)
IRdisplay::display_html(paste("<iframe src=' ", fname, " ' width = 100% height = 400>"))
Let's generate a static map containing the location of all of the airports in the USA
In [22]:
library(ggmap)
map <- get_map(location = "Seattle, WA", zoom = 3)
ggmap(map) + geom_point(aes(x = lon, y = lat), data = usa_airports, alpha = 0.25)
Let's generate a zoomable, pannable map using the leaflet package. Here you can see airports that belong to the USA that aren't geographically within the borders of the United States.
In [23]:
library(leaflet)
m <- leaflet(data = usa_airports) %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addCircles(~lon, ~lat, popup = ~name)
# Trick again - probably should make into a fxn
library(htmlwidgets)
fname = 'tmp.html'
saveWidget(m, file = fname, selfcontained = F)
IRdisplay::display_html(paste("<iframe src=' ", fname, " ' width = 100% height = 400>"))
In [25]:
library(leaflet)
pal <- colorQuantile("YlOrRd", NULL, n = 8)
m <- leaflet(usa_airports) %>%
addTiles() %>%
addCircleMarkers(color = ~pal(lat))
# Trick again just for good measure
library(htmlwidgets)
fname = 'tmp.html'
saveWidget(m, file = fname, selfcontained = F)
IRdisplay::display_html(paste("<iframe src=' ", fname, " ' width = 100% height = 400>"))
Created by a Microsoft Employee based on work by another Microsoft Employee.
The MIT License (MIT)
Copyright (c) 2016 Micheleen Harris
In [ ]: